package com.chkj.fbook.business.service.impl;

import com.chkj.fbook.business.constants.BusConstants;
import com.chkj.fbook.business.dao.SeatDao;
import com.chkj.fbook.business.domain.SeatDO;
import com.chkj.fbook.business.service.SeatService;
import com.chkj.fbook.common.constants.Constants;
import com.chkj.fbook.common.utils.IDUtils;
import com.chkj.fbook.common.utils.RedisUtil;
import com.chkj.fbook.common.utils.TimeUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SeatServiceImpl implements SeatService {

	@Resource
	private SeatDao seatDao;

	@Autowired
	private RedisUtil redisUtil;

	@Override
	public SeatDO get(String id){
		return seatDao.get(id);
	}
	
	@Override
	public List<SeatDO> list(Map<String, Object> map){
		return seatDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return seatDao.count(map);
	}
	
	@Override
	@Transactional
	public int save(SeatDO seat){
		
		//已存在的排序自增
		//seatDao.editAdd1(seat);
		
		seat.setId(IDUtils.genUUID());
		Date date = new Date();
		seat.setCreateTime(date);
		seat.setUpdateTime(date);
		return seatDao.save(seat);
	}
	
	@Override
	@Transactional
	public int update(SeatDO seat){
		
		seat.setUpdateTime(new Date());
		int i = seatDao.update(seat);
		
		return i;
	}
	
	@Override
	@Transactional
	public int remove(String id){
		
		int i = seatDao.remove(id);
		
		//将预约表中大于等于当前时间对应的座位一并删除
		SeatDO seat = new SeatDO();
		seat.setId(id);
		seat.setUpdateTime(new Date());

		return i;
	}
	
	@Override
	public int batchRemove(String[] ids){
		return seatDao.batchRemove(ids);
	}

    @Override
    public List<SeatDO> getAll() {
        return seatDao.getAll();
    }

    @Override
    public List<SeatDO> getByRoom(String roomId) {
        return seatDao.getByRoom(roomId);
    }

	@Override
	public int updateRoomNameByRoomId(SeatDO seat) {
		seat.setUpdateTime(new Date());
		return seatDao.updateRoomNameByRoomId(seat);
	}

	@Override
	public int getSeatCountByRoomId(String roomId) {
		return seatDao.getSeatCountByRoomId(roomId);
	}

    @Override
    public List<SeatDO> getByOrderDateAndRoom(String orderDate, String roomId) {
		String key = Constants.ORDER_SEAT_KEY + orderDate + ":" + roomId;
		Object o = null;
		try {
			o = redisUtil.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (o != null) {
			return (List<SeatDO>) o;
		}
		List<SeatDO> seatDOList = seatDao.getByOrderDateAndRoom(orderDate, roomId);
		if (CollectionUtils.isNotEmpty(seatDOList) && seatDOList.size() > 0) {
			redisUtil.set(key, seatDOList);
		}
		return seatDOList;
    }

    @Override
    public List<SeatDO> getList(String orderDate, String roomId) {
		List<SeatDO> seatList = this.getByOrderDateAndRoom(orderDate, roomId);
		return seatList;
    }

	@Override
	@Transactional
	public int resetState(SeatDO seat) {
		
		//如果是下架  更改下架时间为往后第三天
		if(BusConstants.SEAT_STATE_0==seat.getState()) {
			Date date = TimeUtils.getDateBySpecify(new Date(), 3);
			seat.setLowerShelfTime(date);
		}
		
		seat.setUpdateTime(new Date());
		int i = seatDao.resetState(seat);
		
		return i;
	}

}
